<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Config;
//
//
//use App\Http\Controllers\Controller;
//use App\Role;
//use App\User;
//use App\Permission;
//use Input, Validator, Redirect, Session, Captcha;
//use App\Librarys\Test;


//use EasyWeChat\Foundation\Application;
//use EasyWeChat\Message\Text;
//use EasyWeChat\Message\News;
//use EasyWeChat\Payment\Order;

//use URL;
//use Image;
//use DB,Logok;
use Illuminate\Support\Facades\Cache;
use App\Jobs\OrderJob;
use Carbon\Carbon;
use DB;
use App\Models\Order;
use App\Models\OrderGoods;
class TestController extends Controller {
    


	protected function test(){

		
		
//		$job = (new OrderJob(29))->delay(Carbon::now()->addMinutes(60));
//		dispatch($job);
//		
		
	}


	protected function test7(){
		require (app_path() . '/Librarys/phpqrcode.php');
		$QRcode = new \QRcode;
		ob_start();
		$QRcode->png('http://www.baidu.com');
		$imageString = base64_encode(ob_get_contents());
		ob_end_clean();
		return view('test2',array('aa'=>$imageString));
	}


	protected function test6(){
//		$tplUrl='http://api.map.baidu.com/routematrix/v2/riding?origins='.$order->f_lat.','.$order->f_lng.'&destinations='.$request->s_lat.','.$request->s_lng.'&output=json&ak=W8HqSa1728zR40Yox8PVzxHGXNeF6kgV';
		
		$f_lat = '33.003683';
		$f_lng = '112.560259';
		$s_lat = '32.969693';
		$s_lng = '112.567634';
		$tplUrl='http://api.map.baidu.com/routematrix/v2/riding?origins='.$f_lat.','.$f_lng.'&destinations='.$s_lat.','.$s_lng.'&output=json&ak=W8HqSa1728zR40Yox8PVzxHGXNeF6kgV';
		$a = $this->curlok($tplUrl);
		dump($a);
		$dist = ceil($a['result'][0]['distance']['value']/1000);		
		dump($dist);
	}

	protected function curlok($url){
		// 1. 初始化
		 $ch = curl_init();	
		 // 2. 设置选项，包括URL
		 curl_setopt($ch,CURLOPT_URL,$url);
		 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		 curl_setopt($ch,CURLOPT_HEADER,0);
		 // 3. 执行并获取HTML文档内容
		 $output = curl_exec($ch);//{"status":0,"result":[{"distance":{"text":"1512.8公里","value":1512823},"duration":{"text":"126小时","value":453671}}],"message":"成功"}
		 if($output === FALSE ){
			echo "CURL Error:".curl_error($ch);
		 }
		 // 4. 释放curl句柄
		 curl_close($ch);
		 return json_decode($output,true);
	}

	public function test5(){
		
		$a=100000;
		$b=1000000;
		$c=1;
		$d = 0;

		while ($res = $this->getHb($a, $b, $c,$d)) {
		
			$a = $a-1;
			$b = $res['countMoney'];	
			echo ($res['money']/100).'---';
			if($a<=1){
				 break;
			}						
		}	

			
	}
	
   /*
 * 随机红包
 * $countSize  总个数
 * $countMoney  总钱数
 * $min   最小金额
 * $max   最大金额  传入最大金额时，刚开始时会有大红包，不传入是相对平均
 * 返回 countSize剩余总个数  countMoney 剩余总金额   money红包金额
 */
    private function getHb($countSize ,$countMoney ,$min,$max=0){
        $tem=$countMoney/$countSize;  //平均金额
        if($countSize==1)
            return array("countSize"=>0,"countMoney"=>0,"money"=>$countMoney);  //剩余一个红包时直接返回
        if($max==$min || $tem==$min){
            return array("countSize"=>$countSize-1,"countMoney"=>$countMoney-$min,"money"=>$min);  //最大金额或平均金额等于最小金额时直接返回
        }
        if($max>0) {//传入最大金额时
            $count=floor($max/$min);
            $money=floor(rand(1,$count) *  $max/$count);
        }else{
            $max=$tem*2;
            $money=floor(rand(1,10) *  $max/10);
        }

        if($money<$min)
            $money= $min;
        if($countMoney-$money<$min*$countSize)
            $money=$min;
        return array("countSize"=>$countSize-1,"countMoney"=>$countMoney-$money,"money"=>$money);
    }
	
	
	
	
	public function test4(){
		
//		$img = Image::make('img/foo1.jpg');
//		$img->insert('img/timg.jpg', 'bottom-right', 50, 50);
//		$img->insert('img/timg.jpg', 'center');
//		$img->save('img/foo1.jpg');
		

	}




	
	
	protected function test3(){
		
		

		
//		$app = new Application($options);
//		$server = $app->server;

//		echo "</br>";
//		echo url('test');
//		echo "</br>";
//		echo URL::current();
//		echo "</br>";
		
//		$role = Role::findOrFail(10);
//		$role->users()->sync([]);//删除关联关系
		
//		$user = User::find(60);
//		$user->roles()->detach();//清楚用户角色权限
//		$role = Role::with('perms')->find(12)->toarray();
//		dd($role);
//		$admin = Role::find(13);
//		$admin->perms()->sync([12]);	
		
//		$a = Permission::leftjoin('permission_role',function($join){
//			$join->on('permission_role.permission_id','=','permissions.id')->where('permission_role.role_id','=',12);
//		})
//		->select('permissions.id','permissions.display_name','permissions.pid','permission_role.role_id')
//		->get()->toarray();
//		
//		foreach ($a as $k=>$v) {
//			if($v['role_id']){
//				$a[$k]['checked'] = TRUE;
//			}else{
//				$a[$k]['checked'] = FALSE;
//			}	
//		}

	}
	
	protected function test2(){

		$owner = new Role();
		$owner->name = 'owner';
		$owner->display_name = 'Project Owner';
		$owner->description = 'User is the owner of a given project';
		$owner->save();
		
		$admin = new Role();
		$admin->name = 'admin';
		$admin->display_name = 'User Administrator';
		$admin->description = 'User is allowed to manage and edit other users';
		$admin->save();
		
		$user = User::where('username', '=', 'abc')->first();
		
		//调用EntrustUserTrait提供的attachRole方法
		$user->attachRole($admin); // 参数可以是Role对象，数组或id
		
//		// 或者也可以使用Eloquent原生的方法
//		$user->roles()->attach($admin->id); //只需传递id即可		


		$createPost = new Permission();
		$createPost->name = 'create-post';
		$createPost->display_name = 'Create Posts';
		$createPost->description = 'create new blog posts';
		$createPost->save();
		
		$editUser = new Permission();
		$editUser->name = 'edit-user';
		$editUser->display_name = 'Edit Users';
		$editUser->description = 'edit existing users';
		$editUser->save();
		
		$owner->attachPermission($createPost);
		//等价于 $owner->perms()->sync(array($createPost->id));
		
		$admin->attachPermissions(array($createPost, $editUser));
		//等价于 $admin->perms()->sync(array($createPost->id, $editUser->id));		


	}
	
	
    protected function test1(){


//		$owner = new Role();
//		$owner->name = 'owner';
//		$owner->display_name = 'Project Owner';
//		$owner->description = 'User is the owner of a given project';
//		$owner->save();
//		
//		$admin = new Role();
//		$admin->name = 'admin';
//		$admin->display_name = 'User Administrator';
//		$admin->description = 'User is allowed to manage and edit other users';
//		$admin->save();
//		
//		$user = User::where('name', '=', 'aaa')->first();
//		
//		//调用EntrustUserTrait提供的attachRole方法
//		$user->attachRole($admin); // 参数可以是Role对象，数组或id
//		
////		// 或者也可以使用Eloquent原生的方法
////		$user->roles()->attach($admin->id); //只需传递id即可		
//
//
//		$createPost = new Permission();
//		$createPost->name = 'create-post';
//		$createPost->display_name = 'Create Posts';
//		$createPost->description = 'create new blog posts';
//		$createPost->save();
//		
//		$editUser = new Permission();
//		$editUser->name = 'edit-user';
//		$editUser->display_name = 'Edit Users';
//		$editUser->description = 'edit existing users';
//		$editUser->save();
//		
//		$owner->attachPermission($createPost);
//		//等价于 $owner->perms()->sync(array($createPost->id));
//		
//		$admin->attachPermissions(array($createPost, $editUser));
//		//等价于 $admin->perms()->sync(array($createPost->id, $editUser->id));
		
//		$role = Role::find(21); // 获取给定权限
//		$role->delete();
		
		$role = Role::find(25);
		$a = $role->users();
//		dd($a);
		
		return view('test');

		

    }

	
}
